//
//  ActivityWebVC.m
//  Cunli
//
//  Created by lilubin on 15/8/24.
//  Copyright (c) 2015年 lilubin. All rights reserved.
//

#import "ActivityWebVC.h"
#import "UMSocial.h"
#import "UMSocialWechatHandler.h"
#import "UMSocialQQHandler.h"
#import "HYActivityView.h"
#import "MyDataService.h"
#import "NSString+MD5.h"
#import "PointsManager.h"

@interface ActivityWebVC ()  <UIWebViewDelegate, UIScrollViewDelegate>
{
    NSDictionary *_activityData;
}

@property (nonatomic, strong) UIWebView *activityWebView;   //活动webview
@property (nonatomic, strong) HYActivityView *activityView;


@end

@implementation ActivityWebVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"详情";
    [self setAttributesAboutWebView];
    [self loadActivity];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setAttributesAboutWebView
{
    CGRect newFrame = self.view.frame;
    CGFloat newHeight = newFrame.size.height-64;
    newFrame.size.height = newHeight;
    
    _activityWebView = [[UIWebView alloc] initWithFrame:newFrame];
    [_activityWebView setBackgroundColor:[UIColor colorWithHexString:UIBgColorValue]];
    _activityWebView.delegate = self;
    _activityWebView.scrollView.delegate = self;
    _activityWebView.scrollView.bounces = YES;
    _activityWebView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:_activityWebView];
    
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:_activityUrl]];
    [_activityWebView loadRequest:request];
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(0, 0, 25, 20);
    [rightBtn setImage:[UIImage imageNamed:@"nav_right_share"] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(popAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    self.navigationItem.rightBarButtonItem = rightBarItem;

}

- (void)loadActivity
{
    
    NSMutableDictionary *CheckParams = [NSMutableDictionary dictionaryWithCapacity:5];
    [CheckParams setObject:[Config getOwnUserID] forKey:@"userid"];
    [CheckParams setObject:_activityId forKey:@"act_id"];
    NSString *resultSign = [NSString getSignFromParam:CheckParams andSession:[Config getOwnSession]];
    [CheckParams setObject:resultSign forKey:@"sign"];
    
    [MyDataService requestURL:@"active_share.php" httpMethod:@"GET" params:CheckParams fileDatas:nil complection:^(id result) {
        NSLog(@"ActivityShare Result:%@", result);
        if ([result[@"response"] isEqualToString:@"active_share"]) {
            _activityData = result[@"data"];
        }
    } failureAction:^{
        [Config showHUDWithText:@"网络错误" andImage:@"error"];
    }];
}

- (void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSLog(@"---url:%@", request.URL.absoluteString);
    return YES;
}

-(void)dealloc
{
    _activityWebView = nil;
}


-(void)popAction:(UIButton *)button
{
    NSString *shareTitle;
//    NSString *shareUrl;
    NSString *shareIntroduce;
    if (_activityData != nil) {
        shareTitle = _activityData[@"title"];
//        shareUrl = _activityData[@"share_url"];
        shareIntroduce = _activityData[@"introduce"];
    }
    
    if (!self.activityView) {
        self.activityView = [[HYActivityView alloc]initWithTitle:@"分享到" referView:self.view];
        
        //横屏会变成一行6个, 竖屏无法一行同时显示6个, 会自动使用默认一行4个的设置.
        self.activityView.numberOfButtonPerLine = 6;
        
        ButtonView *bv = [[ButtonView alloc]initWithText:@"QQ" image:[UIImage imageNamed:@"cunli_qqFriend"] handler:^(ButtonView *buttonView){
            NSLog(@"点击QQ");
            
            [UMSocialData defaultData].extConfig.qqData.qqMessageType = UMSocialQQMessageTypeDefault;
            [UMSocialData defaultData].extConfig.qqData.title = shareTitle;
            [UMSocialData defaultData].extConfig.qqData.url = _activityUrl;
            
            [[UMSocialDataService defaultDataService] postSNSWithTypes:@[UMShareToQQ] content:_activityUrl image:nil location:nil urlResource:nil presentedController:self completion:^(UMSocialResponseEntity *response) {
                if (response.responseCode == UMSResponseCodeSuccess) {
                    NSLog(@"QQ分享成功");
                    //获取分享积分
                    [PointsManager sendSharePointWithChannel:@"4" andItemid:[NSString stringWithFormat:@"%@",_activityId]];
                }
            }];
            
        }];
        [self.activityView addButtonView:bv];
        
        bv = [[ButtonView alloc]initWithText:@"QQ空间" image:[UIImage imageNamed:@"cunli_qqZone"] handler:^(ButtonView *buttonView){
            //            NSLog(@"点击QQ空间");
            [UMSocialData defaultData].extConfig.qqData.qqMessageType = UMSocialQQMessageTypeDefault;
            [UMSocialData defaultData].extConfig.qzoneData.title = shareTitle;
            [UMSocialData defaultData].extConfig.qzoneData.url = _activityUrl;
            
            [[UMSocialDataService defaultDataService] postSNSWithTypes:@[UMShareToQzone] content:shareIntroduce image:[UIImage imageNamed:@"aboutApp01.png"] location:nil urlResource:nil presentedController:self completion:^(UMSocialResponseEntity *response) {
                if (response.responseCode == UMSResponseCodeSuccess) {
                    //                    NSLog(@"QQ空间分享成功");
                    [PointsManager sendSharePointWithChannel:@"3" andItemid:[NSString stringWithFormat:@"%@",_activityId]];
                }
            }];
            
        }];
        [self.activityView addButtonView:bv];
        
        bv = [[ButtonView alloc]initWithText:@"微信" image:[UIImage imageNamed:@"cunli_wechatFriend"] handler:^(ButtonView *buttonView){
            
            //            NSLog(@"点击微信");
            
            [UMSocialData defaultData].extConfig.wxMessageType = UMSocialWXMessageTypeWeb;
            [UMSocialData defaultData].extConfig.wechatSessionData.url = _activityUrl;
            [UMSocialData defaultData].extConfig.title = shareTitle;
            
            [[UMSocialDataService defaultDataService] postSNSWithTypes:@[UMShareToWechatSession] content:shareIntroduce image:nil location:nil urlResource:nil presentedController:self completion:^(UMSocialResponseEntity *response) {
                //                NSLog(@"分享成功");
                
                [PointsManager sendSharePointWithChannel:@"1" andItemid:[NSString stringWithFormat:@"%@",_activityId]];
            }];
        }];
        [self.activityView addButtonView:bv];
        
        bv = [[ButtonView alloc]initWithText:@"朋友圈" image:[UIImage imageNamed:@"cunli_wechatTimeline"] handler:^(ButtonView *buttonView){
            //            NSLog(@"朋友圈");
            
            [UMSocialData defaultData].extConfig.wxMessageType = UMSocialWXMessageTypeWeb;
            [UMSocialData defaultData].extConfig.wechatTimelineData.url = _activityUrl;
            [UMSocialData defaultData].extConfig.title = shareTitle;
            
            [[UMSocialDataService defaultDataService] postSNSWithTypes:@[UMShareToWechatTimeline] content:shareIntroduce image:nil location:nil urlResource:nil presentedController:self completion:^(UMSocialResponseEntity *response) {
                //NSLog(@"分享成功");
                [PointsManager sendSharePointWithChannel:@"2" andItemid:[NSString stringWithFormat:@"%@",_activityId]];
            }];
            
        }];
        [self.activityView addButtonView:bv];
        }
    [self.activityView show];
    
}






@end
